package org.buptsse.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.os.Message;

public class ConnectionChecker {

	/**
	
	 * 判断网络功能是否可用
	
	 * 需要权限< uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">
	
	 * @param ctx
	
	 * @return
	
	 */
	
	public static boolean isNetworkAvailable(Context ctx) {
		ConnectivityManager cm = (ConnectivityManager) ctx
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cm.getActiveNetworkInfo();
		return (info != null && info.isConnected());
	}
	
	public static boolean CheckConnection(Context ctx,Handler handler)
	{
		boolean isConnected=false;
		ConnectivityManager cm = (ConnectivityManager) ctx
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cm.getActiveNetworkInfo();
		isConnected=(info != null && info.isConnected());
		if(isConnected==false)
		{
			Message msg=handler.obtainMessage(APPConstant.NetUnavailable);
			handler.sendMessage(msg);
		}
		return isConnected;
	}

}
